#ifndef __TASK_H__
#define __TASK_H__
#include "types.h"
#include "kern_conf.h"


#include "list.h"
#include <stdlib.h>

//#include "time.h"

#define IDLE_PRIO MAX_TASKS - 1


typedef struct task_struct {
	OSStackType *TopOfStkPtr;  /* Top of task's stack*/
	uint_16 prio;      	/* Priority of corresponding task */
	state_t state; 		/* State of task */
	event_state_t estate;
	uint_32 delay;
	struct list_head event_list;
	struct list_head delay_list;
} TCB;

typedef struct task_alt {
	unsigned int r[13];
	unsigned int SP;
	unsigned int LR;
	unsigned int xPSR;
} TCBalt;


extern TCB *TaskArray[MAX_TASKS];
#if HIGHEST_PRIO_ALT == 1
extern TCB *ReadyArray[MAX_TASKS];
#elif HIGHEST_PRIO_ALT == 2
extern uint_32 ReadyTaskBitmap[(MAX_TASKS >> 5) + 1];
extern uint_32 ReadyTaskIndex;
#elif HIGHEST_PRIO_ALT == 3
extern uint_32 ReadyTaskBitmap;
#endif

extern struct list_head allTasksLinked;


err_t task_create(uint_8 prio, task function, void *args, OSStackType stack_size, OSStackType *tos);
err_t TaskEnable(uint_16 prio);
TCB * TCBAlloc(void);
void TCBInit(TCB *newTCB, uint_8 prio, uint_32 stk_size);
void tasksInit(void);
TCB *getTCBbyPrio(unsigned int prio);
#if HIGHEST_PRIO_ALT != 3
void FindHighestPriorityTask(void);
#endif

void schedule(void);
void yield(void);


extern TCB *currentTCB;
extern TCB *highestTCB;
extern TCB *currentTCBalt;
extern TCB *highestTCBalt;



#endif
